<template>
  <div>
    <el-breadcrumb>
      <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户信息管理</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="main-usermessage">
      <el-card>
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <p class="head">用户信息修改</p>
          <el-form-item label="用户名" prop="username">
            <el-input v-model.number="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model.number="ruleForm.email"></el-input>
          </el-form-item>
          <el-form-item label="个性签名" prop="personNote">
            <el-input v-model.number="ruleForm.personNote"></el-input>
          </el-form-item>
          <el-form-item label="原密码" prop="oldpass">
            <el-input
              type="password"
              v-model="ruleForm.oldpass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="pass">
            <el-input
              type="password"
              v-model="ruleForm.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input
              type="password"
              v-model="ruleForm.checkPass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >提交</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
const Base64 = require('js-base64').Base64
export default {
  name: 'userMessage',
  data() {
    const checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'))
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    const validateOldPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入原密码'))
      } else {
        this.$http.get(`/users/${this.ruleForm.id}`).then((res) => {
          const { password } = res.data.result
          if (value === password) {
            callback()
          } else {
            callback(new Error('原密码输入错误,请重新输入'))
          }
        })
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        id: 0,
        pass: '',
        checkPass: '',
        username: '',
        email: '',
        personNote: '',
      },
      rules: {
        oldpass: [
          { required: true, validator: validateOldPass, trigger: 'blur' },
        ],
        email: [
          {
            required: true,
            type: 'email',
            message: '请输入正确的邮箱地址',
            trigger: 'blur',
          },
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' },
          {
            min: 3,
            max: 16,
            message: '密码在3到16位之间',
            trigger: 'blur',
          },
        ],
        checkPass: [{ validator: validatePass2, trigger: 'blur' }],
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 16,
            message: '用户名长度在 3 到 16 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  created() {
    const payload = localStorage.getItem('token').split('.')[1]
    this.ruleForm.id = JSON.parse(Base64.decode(payload)).id
    this.$api.getOneUser(this.ruleForm.id).then((res) => {
      const { username, email, personNote } = res.data.result
      this.ruleForm.username = username
      this.ruleForm.email = email
      this.ruleForm.personNote = personNote
    })
    // this.getuserInfo()
  },
  methods: {
    getuserInfo() {},
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$api.putCurrentUser(this.ruleForm).then((res) => {
            localStorage.removeItem('token')
            this.$message.success(res.data.message)
            this.$router.replace('/login')
          })
        } else {
          console.log('error submit!!')
          this.$message.error('表单验证不通过')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
  },
}
</script>

<style scoped>
.main-usermessage {
  height: 80vh;
  margin-top: 10px;
  padding-top: 10px;
  background-color: #eee;
}
.el-card {
  /* height: 400px; */
  width: 500px;
  margin: 10px auto;
}
.head {
  padding-bottom: 30px;
  text-align: center;
  font-size: 25px;
  font-family: '华文行楷';
}
</style>
